package com.yanqu.road.logic.xlogic.user;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class XUserBaseInfoLogic {

    private static Logger logger = LogManager.getLogger(XUserBaseInfoLogic.class.getName());

    // 活动ID，condition类型，玩家ID，上次上传的玩家信息
    private static Map<Integer, Map<Integer, Map<Long, XUserBaseInfo>>> updateInfoMap = new ConcurrentHashMap<>();

    public static PlayerProto.PlayerBaseTempMsg.Builder getUpdateInfoBuilder(int activityId, int condType, long userId, UserBaseInfo baseInfo) {
        XUserBaseInfo updateInfo = getUpdateUserBaseInfo(activityId, condType, userId);
        return updateInfo.getUpdateMsg(baseInfo);
    }

    public static void setNextAll(int activityId, int condType, long userId) {
        XUserBaseInfo updateInfo = getUpdateUserBaseInfo(activityId, condType, userId);
        updateInfo.setLastAllTime(0L);
    }

    private static XUserBaseInfo getUpdateUserBaseInfo(int activityId, int condType, long userId) {
        // activityId
        Map<Integer, Map<Long, XUserBaseInfo>> map1 = updateInfoMap.get(activityId);
        if (map1 == null) {
            synchronized (updateInfoMap) {
                updateInfoMap.putIfAbsent(activityId, new ConcurrentHashMap<>());
            }
            map1 = updateInfoMap.get(activityId);
        }

        // condType
        Map<Long, XUserBaseInfo> map2 = map1.get(condType);
        if (map2 == null) {
            synchronized (map1) {
                map1.putIfAbsent(condType, new ConcurrentHashMap<>());
            }
            map2 = map1.get(condType);
        }

        // userId
        XUserBaseInfo updateInfo = map2.get(userId);
        if (updateInfo == null) {
            synchronized (map2) {
                map2.putIfAbsent(userId, new XUserBaseInfo(userId));
            }
            updateInfo = map2.get(userId);
        }

        return updateInfo;
    }

    public static void clearOldData() {

        // 清空3小时没变化的数据
        long clearTime = System.currentTimeMillis() - 3 * DateHelper.HOUR_MILLIONS;

        // 遍历活动ID
        for (Map.Entry<Integer, Map<Integer, Map<Long, XUserBaseInfo>>> entry1 : updateInfoMap.entrySet()) {

            int activityId = entry1.getKey();
            Map<Integer, Map<Long, XUserBaseInfo>> conditionMap = entry1.getValue();

            // 遍历conditionType
            for (Map.Entry<Integer, Map<Long, XUserBaseInfo>> entry2 : conditionMap.entrySet()) {
                int conditionType = entry2.getKey();

                // 遍历userId
                boolean clear = true;
                for (XUserBaseInfo xUserBaseInfo : entry2.getValue().values()) {
                    if (xUserBaseInfo.getLastAllTime() > clearTime) {
                        clear = false;
                        break;
                    }
                }

                if (clear) {
                    conditionMap.remove(conditionType);
                    logger.error("clear updateInfo data activityId {} conditionType {}.", activityId, conditionType);
                }
            }

            if (conditionMap.isEmpty()) {
                updateInfoMap.remove(activityId);
                logger.error("activityId {} updateInfo data all empty.", activityId);
            }
        }
    }
}
